<!DOCTYPE html>
<html lang="zh"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head lang="zh">
    <meta charset="utf-8">
    <title>SPU管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/font.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body class="ebe-body">
<div class="layui-fluid  ebe-table" id="ebe-spu">
    <div class="layui-card">
        <div class="layui-card-body ebe-table-full">
            <form class="layui-form ebe-search">
                <div class="layui-row">
                    <div class="layui-col-md10">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label layui-form-label-sm">标题</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="title"  autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label layui-form-label-sm">状态</label>
                                <div class="layui-input-inline">
                                    <select name="status">
                                        <option value=""></option>
                                        <option value="1">上架</option>
                                        <option value="0">下架</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm12 layui-col-xs12  table-action-area">
                        <div class="layui-btn layui-btn-sm layui-btn-primary table-action" lay-submit
                             lay-filter="data-search-btn" id="query">
                            <i class="layui-icon layui-icon-search"></i>
                        </div>
                        <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                             shiro:hasAnyPermissions="spu:add,spu:delete">
                            <i class="layui-icon layui-icon-unorderedlist"></i>
                        </div>
                    </div>
                </div>
            </form>

            <table class="layui-hide" id="spuTable" lay-filter="currentTableFilter"></table>
            <script type="text/html" id="currentTableBar">
                <span shiro:lacksPermission="spu:update,spu:delete">
        				<span class="layui-badge-dot"></span> 无权限
    				</span>
                <a shiro:hasPermission="spu:update" class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                <a shiro:hasPermission="spu:delete" class="layui-btn layui-btn-xs layui-btn-red data-count-delete" lay-event="delete">删除</a>
            </script>
        </div>
    </div>


</div>
</div>
<script src="/lib/layui/layui.js" charset="utf-8"></script>
<script src="/lib/lay-config.js" charset="utf-8"></script>
<script type="text/html" id="spu-status">
    {{#
    var status = {
    1: {title: '上架', color: 'green'},
    0: {title: '下架', color: 'red'}
    }[d.status];
    }}
    <span class="ebe-badge ebe-tag-{{status.color}}">{{ status.title }}</span>
</script>
<script data-th-inline="none">
    layui.use(['form', 'table', 'layuimini', 'dropdown', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layuimini = layui.layuimini,
            dropdown = layui.dropdown,
            table = layui.table;

        //表格
        layuimini.table.init({
            elem: '#spuTable',
            url: '/business/spu/list',
            cols: [[
                {type: "checkbox", width: 50, fixed: "left"},
                {field: 'spuId', title: 'ID', align: "center",width:50},
                {field: 'cover', title: '主图', align: "center",templet:function (d) {
                    if (d.cover !== undefined) {
                        return '<div ><img class="ebe-table-img" src="'+d.cover+'"></div>';
                    }
                    return '';
                }},
                {field: 'title', title: '标题',align: "center", minWidth: 100},
                {field: 'originPrice', title: '价格',align: "center", sort: true,templet:'<div>¥{{d.originPrice}}</div>'},
                {field: 'categoryName', title: '分类',align: "center"},
                {field: 'status', title: '状态',align: "center",templet:"#spu-status"},
                {field: 'createTime', title: '创建时间', align: "center", minWidth: 180, sort: true},
                {title: '操作', minWidth: 180, fixed: "right", align: "center", templet: "#currentTableBar"}
            ]],
        });
        dropdown.render({
            elem: $('.action-more'),
            click: function (name, elem, event) {
                var checkStatus = table.checkStatus('spuTable');
                let checkLen = checkStatus.data.length;
                if (name === 'add') {
                    layuimini.model.openMax('新增SPU', '/business/spu/add');
                }
                if (name === 'delete') {
                    if (!checkLen) {
                        layuimini.alert.warn('请选择需要删除的SPU');
                    } else {
                        layuimini.modal.confirm('删除SPU', '确定删除所选SPU？', function () {
                            let spuIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                spuIds.push(item.spuId)
                            });
                            layuimini.delete('/business/spu/' + spuIds.join(','),function() {
                                table.reload('spuTable', {
                                    page: {
                                        curr: layuimini.judgeRemoveTablePage('spuTable',checkLen)
                                    }
                                }, 'data');
                                layuimini.alert.success('删除spu成功');
                            });
                        });
                    }
                }
            },
            options: [{
                name: 'add',
                title: '新增SPU',
                perms: 'spu:add'
            }, {
                name: 'delete',
                title: '删除SPU',
                perms: 'spu:delete'
            }]
        });
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = data.field;
            //执行搜索重载
            table.reload('spuTable', {
                page: {
                    curr: 1
                },
                where: result

            }, 'data');

            return false;
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'delete') {
                layuimini.modal.confirm('删除SPU', '确定删除该SPU？', function () {
                    layuimini.delete('/business/spu/' + data.spuId,function() {
                        table.reload('spuTable', {
                            page: {
                                curr: layuimini.judgeRemoveTablePage('spuTable',1)
                            }
                        }, 'data');
                        layuimini.alert.success('删除规格组成功');
                    });
                });
            }
            if (layEvent === 'edit') {
                layuimini.model.openMax('修改SPU', '/business/spu/update/' + data.spuId);
            }
        });

    });
</script>
<script>

</script>

</body>
</html>
